<template>
  <el-dialog title="详情" :close-on-click-modal="false" :visible.sync="visible">
    <el-button
      v-if="isAuth('ed:exam:delete')"
      type="danger"
      @click="deleteHandle()"
      :disabled="scoreListSelections.length <= 0"
      style="margin-bottom: 20px;"
      >批量删除</el-button>
    <el-table
      :data="scoreList"
      border
      v-loading="scoreListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        label="学号">
      </el-table-column>
      <el-table-column
        prop="nickname"
        header-align="center"
        align="center"
        label="姓名">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        label="考试时间">
      </el-table-column>
      <el-table-column
        prop="label"
        header-align="center"
        align="center"
        label="考试内容">
      </el-table-column>
      <el-table-column
        prop="hp"
        header-align="center"
        align="center"
        label="剩余血量">
      </el-table-column>
      <el-table-column
        prop="score"
        header-align="center"
        align="center"
        label="成绩">
      </el-table-column>
      <el-table-column
        prop="pass"
        header-align="center"
        align="center"
        label="是否通过">
        <template slot-scope="scope">
          <span v-if="scope.row.pass">通过</span>
          <span v-else>未通过</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('ed:exam:delete')" type="text" size="small" @click="deleteHandle(scope.row.examId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      pageIndex: 1,
      pageSize: 10,
      scoreList: [],
      totalPage: 0,
      scoreListSelections: [],
      scoreListLoading: false,
      userId: ''
    }
  },
  watch: {
    'visible': function (val) {
      if (!val) {
        this.pageIndex = 1
      }
    }
  },
  methods: {
    init (id) {
      this.userId = id
      this.visible = true
      this.scoreListLoading = true
      this.$http({
        url: this.$http.adornUrl('/ed/exam/list'),
        method: 'get',
        params: this.$http.adornParams({
          'current': this.pageIndex,
          'size': this.pageSize,
          'userId': id
        })
      }).then(({data}) => {
        if (data && data.code === '0') {
          this.scoreList = data.data.records
          this.totalPage = data.data.total
        } else {
          this.scoreList = []
          this.totalPage = 0
        }
        this.scoreListLoading = false
      })
    },
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.init(this.userId)
    },
    currentChangeHandle (val) {
      this.pageIndex = val
      this.init(this.userId)
    },
    // 多选
    selectionChangeHandle (val) {
      this.scoreListSelections = val
    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.scoreListSelections.map(item => {
        return item.examId
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/ed/exam/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === '0') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.pageIndex = 1
                this.init(this.userId)
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    }
  }
}
</script>